/* $Cambridge: exim/exim-src/src/lookups/spf.c,v 1.3 2009/11/11 14:43:28 nm4 Exp $ */

/*************************************************
*     Exim - an Internet mail transport agent    *
*************************************************/

/*
 * Exim - SPF lookup module using libspf2
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * Copyright (c) 2005 Chris Webb, Arachsys Internet Services Ltd
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 */

#include "../exim.h"

#ifndef EXPERIMENTAL_SPF
static void dummy(int x) { dummy(x-1); }
#else

#include "lf_functions.h"
#include "spf.h"
#ifndef HAVE_NS_TYPE
#define HAVE_NS_TYPE
#endif
#include <spf2/spf.h>
#include <spf2/spf_dns_resolv.h>
#include <spf2/spf_dns_cache.h>

void *spf_open(uschar *filename, uschar **errmsg) {
  SPF_server_t *spf_server = NULL;
  spf_server = SPF_server_new(SPF_DNS_CACHE, 0);
  if (spf_server == NULL) {
    *errmsg = US"SPF_server_new() failed";
    return NULL;
  }
  return (void *) spf_server;
}

void spf_close(void *handle) {
  SPF_server_t *spf_server = handle;
  if (spf_server) SPF_server_free(spf_server);
}

int spf_find(void *handle, uschar *filename, uschar *keystring, int key_len,
             uschar **result, uschar **errmsg, BOOL *do_cache) {
  SPF_server_t *spf_server = handle;
  SPF_request_t *spf_request = NULL;
  SPF_response_t *spf_response = NULL;

  spf_request = SPF_request_new(spf_server);
  if (spf_request == NULL) {
    *errmsg = US"SPF_request_new() failed";
    return FAIL;
  }

  if (SPF_request_set_ipv4_str(spf_request, CS filename)) {
    *errmsg = string_sprintf("invalid IP address '%s'", filename);
    return FAIL;
  }
  if (SPF_request_set_env_from(spf_request, CS keystring)) {
    *errmsg = string_sprintf("invalid envelope from address '%s'", keystring);
    return FAIL;
  }

  SPF_request_query_mailfrom(spf_request, &spf_response);
  *result = string_copy(US SPF_strresult(SPF_response_result(spf_response)));
  SPF_response_free(spf_response);
  SPF_request_free(spf_request);
  return OK;
}

#endif /* EXPERIMENTAL_SPF */
